var RepairRecordForm = function () {
  var RepairRecordFormModel = function (repair, type) {
    BaseModel.call(this);

    var self = this;
    var fn = {
      initData: function (repair, type) {
        if (type === undefined) {
          type = 'add';
        }

        if (repair === undefined) {
          repair = {};
        }
        var fields = ['machineModel', 'workBrokenPart', 'workBrokenDescription', 'workBrokenSolveWay'];
        self.setDataFromObj(repair, fields, '');

        if (type === 'add') {
          self.status = 1;
        } else {
          self.id = repair.id;
          self.status = repair.status;
        }
      }
    };

    this.checkValid = function () {
      var fields = ['machineModel', 'workBrokenPart', 'workBrokenDescription', 'workBrokenSolveWay'];
      return this.checkRequiredFields(fields, true);
    };

    this.getObject = function () {
      var postData = {};
      var fields = ['machineModel', 'workBrokenPart', 'workBrokenDescription', 'workBrokenSolveWay', 'status'];
      this.setPostData(postData, fields);

      var id = this.id;
      if (!SysUtil.isEmpty(id)) {
        postData.id = id;
      }
      return postData;
    };

    fn.initData(repair, type);
  };

  var template = /* html */ `
    <div class="repair-record-form" v-loading.fullscreen.lock="loading">
      <div>
        <el-form size="medium" label-width="auto">
          <el-form-item label="设备型号">
            <el-input v-model="repairForm.machineModel" class="record-form-input"></el-input>
          </el-form-item>
          <el-form-item label="故障部件">
            <el-select filterable v-model="repairForm.workBrokenPart" class="record-form-input">
              <el-option v-for="part in workBrokenPartList" :key="part" :value="part"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="故障描述">
            <el-input v-model="repairForm.workBrokenDescription" type="textarea" :autosize="{minRows:1}" resize="none"></el-input>
          </el-form-item>
          <el-form-item label="解决方案">
            <el-input v-model="repairForm.workBrokenSolveWay" type="textarea" :autosize="{minRows:1}" resize="none"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="repairForm.status" class="record-form-input">
              <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:center">
        <el-button size="medium" type="primary" @click="ensure">确定</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      repair: Object,
      type: {
        type: String,
        default: 'add'
      },
      dropdownOption: {
        type: Object,
        default: function () {
          return {};
        }
      }
    },

    data: function () {
      var repair = this.repair;
      var repairForm;
      if (!SysUtil.isEmpty(repair)) {
        repairForm = new RepairRecordFormModel(repair, this.type);
      } else {
        repairForm = new RepairRecordFormModel();
      }
      return {
        loading: false,
        repairForm: repairForm,
        workBrokenPartList: [],
        statusList: i18n.getDropDown('machineRepair.repairRecord.status', [1, 0])
      };
    },

    created: function () {
      this.loading = true;
      var self = this;
      this.setWorkBrokenPartList().then(function () {
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      setWorkBrokenPartList: function () {
        var dropdownOption = this.dropdownOption;
        var workBrokenPartList = dropdownOption.workBrokenPartList;
        if (workBrokenPartList !== undefined) {
          this.workBrokenPartList = workBrokenPartList;
          return Promise.resolve(workBrokenPartList);
        }

        var self = this;
        return ajaxRequest('html/createOrder/json/brokenPart.json', {}, false).then(function (res) {
          var workBrokenPartList = res.rows;
          workBrokenPartList = _.map(workBrokenPartList, 'text');
          dropdownOption.workBrokenPartList = workBrokenPartList;
          self.workBrokenPartList = workBrokenPartList;
          return workBrokenPartList;
        });
      },

      ensure: function () {
        var repairForm = this.repairForm;
        var code = repairForm.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`machineRepair.repairRecord.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        if (this.type === 'add') {
          this.addRepair(repairForm);
        } else {
          this.updateRepair(repairForm);
        }
      },

      addRepair: function (repairForm) {
        var postData = repairForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/saveWorkOrderInfoForCustmerOperate', postData).then(function () {
          self.loading = false;
          self.$alert('维修记录新增成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      updateRepair:function(repairForm){
        var postData = repairForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/saveWorkOrderInfoForCustmerOperate', postData).then(function () {
          self.loading = false;
          self.$alert('维修记录修改成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
